<template>
  <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" :collapse="isCollapse"
           background-color="#545c64"
           text-color="#fff"
           active-text-color="#ffd04b">
    <h3>{{getHead(getPermission)}}</h3>
    <el-menu-item @click="clickMenu(item)" v-for="item in noChildren" :key="item.name"  :index="item.name">
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.name" >
        <el-menu-item @click="clickMenu(subItem)" :index="subItem.name">{{ subItem.label }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
.el-menu {
  min-height: 100vh;
  height: 100%;
  border-right: 0;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 24px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
import Path from '../router/path'
import Head from '../router/Head'
export default {
  data () {
    return {
      menuData: Path.menuData,
      head: Head.head
    }
  },
  methods: {
    handleOpen (key, keyPath) {
    },
    handleClose (key, keyPath) {
    },
    clickMenu (item) {
      if (this.$route.path !== item.path) {
        this.$router.push(item.path)
      }
    }
  },
  computed: {
    noChildren () {
      let permission = JSON.parse(localStorage.getItem('userInfo')).type
      return this.menuData.filter(item => !item.children && (item.permission.includes(permission) || item.permission[0] === 'all'))
    },
    hasChildren () {
      let permission = JSON.parse(localStorage.getItem('userInfo')).type
      return this.menuData.filter(item => item.children && (item.permission.includes(permission) || item.permission[0] === 'all'))
    },
    getHead () {
      return (permission) => {
        let data = this.head.find((role) => role.permission[0] === permission)
        if (this.isCollapse) {
          return data.shortTitle
        } else {
          return data.title
        }
      }
    },
    isCollapse () {
      return this.$store.state.tab.isCollapse
    },
    getPermission () {
      const userInfo = localStorage.getItem('userInfo')
      return JSON.parse(userInfo).type
    }
  }
}
</script>
